<template>
    <div class="div">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="客户名称" prop="client_name">
                <el-input type="text" v-model="ruleForm.client_name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="公司地址" prop="company_adress">
                <el-input type="text" v-model="ruleForm.company_adress" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="联系人姓名" prop="linkman">
                <el-input type="text" v-model="ruleForm.linkman" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="联系人电话" prop="linkman_phone">
                <el-input type="text" v-model="ruleForm.linkman_phone" autocomplete="off"></el-input>
            </el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')" v-if="!ruleForm.client_name">添加</el-button>
            <el-button type="primary" @click="update('ruleForm')" v-else>完成编辑</el-button>
        </el-form>
    </div>
</template>

<script>
import VueCropper from 'vue-cropperjs';
import { add_client, put_client } from '../../api/api.js';
export default {
    name: 'upload',
    data: function() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入客户名称'));
            } else {
                if (this.ruleForm.password !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入公司地址'));
            } else {
                callback();
            }
        };
        var validatePass3 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入联系人电话'));
            } else {
                if (!/^1[3456789]\d{9}$/.test(value)) {
                    callback(new Error('手机号码有误，请重填'));
                } else {
                    this.is_phone = true;
                    callback();
                }
            }
        };
        var validatePass5 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入联系人姓名'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                client_name: '',
                company_adress: '',
                linkman: '',
                linkman_phone: ''
            },
            rules: {
                client_name: [{ validator: validatePass, trigger: 'blur' }],
                company_adress: [{ validator: validatePass2, trigger: 'blur' }],
                linkman: [{ validator: validatePass5, trigger: 'blur' }],
                linkman_phone: [{ validator: validatePass3, trigger: 'blur' }]
            },
            cid: ''
        };
    },
    components: {
        VueCropper
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    add_client({
                        client_name: this.ruleForm.client_name,
                        company_adress: this.ruleForm.company_adress,
                        linkman: this.ruleForm.linkman,
                        linkman_phone: this.ruleForm.linkman_phone
                    }).then(res => {
                        this.$message(res.data.msg);
                        this.$router.push('/client');
                    });
                } else {
                    return false;
                }
            });
        },
        update(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    put_client({
                        client_id: this.cid,
                        client_name: this.ruleForm.client_name,
                        company_adress: this.ruleForm.company_adress,
                        linkman: this.ruleForm.linkman,
                        linkman_phone: this.ruleForm.linkman_phone
                    }).then(res => {
                        this.$message(res.data.msg);
                        this.$router.push('/client');
                    });
                } else {
                    return false;
                }
            });
        }
    },
    created() {
        // this.cropImg = this.defaultSrc;
    },
    mounted: function() {
        var data = this.$route.query;
        this.cid = data.cid;
        this.ruleForm.client_name = data.client_name;
        this.ruleForm.company_adress = data.company_adress;
        this.ruleForm.linkman = data.linkman;
        this.ruleForm.linkman_phone = data.linkman_phone;
    }
};
</script>

<style scoped>
.content-title {
    font-weight: 400;
    line-height: 50px;
    margin: 10px 0;
    font-size: 22px;
    color: #1f2f3d;
}
.pre-img {
    width: 100px;
    height: 100px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 5px;
}
.crop-demo {
    display: flex;
    align-items: flex-end;
}
.crop-demo-btn {
    position: relative;
    width: 100px;
    height: 40px;
    line-height: 40px;
    padding: 0 20px;
    margin-left: 30px;
    background-color: #409eff;
    color: #fff;
    font-size: 14px;
    border-radius: 4px;
    box-sizing: border-box;
}
.crop-input {
    position: absolute;
    width: 100px;
    height: 40px;
    left: 0;
    top: 0;
    opacity: 0;
    cursor: pointer;
}
.div {
    margin: auto;
    width: 400px;
}
</style>